package com.paishen.peiniwan.frame.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.paishen.peiniwan.R;

/**
 * Created by Administrator on 2015/12/1.
 */
public class InputEditLayout extends RelativeLayout {

    private final EditText edInput;
    private final ImageView imgIcon;
    private final View viewLine;

    private int iconFocus, iconNormal;

    public InputEditLayout(Context context) {
        this(context, null);
    }

    public InputEditLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.layout_input_edit, this);
        edInput = (EditText) findViewById(R.id.ed_input);
        imgIcon = (ImageView) findViewById(R.id.img_icon);
        viewLine = findViewById(R.id.view_line);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.InputEditLayout);

        int edInputMaxLength = ta.getInt(R.styleable.InputEditLayout_maxLength, -1);
        if (edInputMaxLength > 0) {
            edInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(edInputMaxLength)});
        }

        final String edInputHint = ta.getString(R.styleable.InputEditLayout_hint);
        if (!TextUtils.isEmpty(edInputHint)) {
            edInput.setHint(edInputHint);
        }

        boolean security = ta.getBoolean(R.styleable.InputEditLayout_security, false);
        if (security) {
            edInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }

        iconFocus = ta.getResourceId(R.styleable.InputEditLayout_iconFocus, -1);
        iconNormal = ta.getResourceId(R.styleable.InputEditLayout_iconNormal, -1);
        if (iconNormal > 0) {
            imgIcon.setImageResource(iconNormal);
            imgIcon.setVisibility(View.VISIBLE);
        } else {
            imgIcon.setVisibility(View.GONE);

            MarginLayoutParams mlp = (MarginLayoutParams) edInput.getLayoutParams();
            final float scale = context.getResources().getDisplayMetrics().density;
            int spacing = (int) (7 * scale + 0.5f);
            mlp.setMargins(spacing, 0, 0, 0);
            edInput.setLayoutParams(mlp);
        }

        ta.recycle();

        edInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                boolean emptyText = TextUtils.isEmpty(edInput.getText());
                viewLine.setBackgroundColor(!emptyText ? 0xFF05131A : 0xFFB3B3B3);
                if (iconNormal > 0) {
                    imgIcon.setImageResource(!emptyText ? iconFocus : iconNormal);
                }
            }
        });
    }

    public void addTextChangedListener(TextWatcher watcher) {
        edInput.addTextChangedListener(watcher);
    }

    /**
     * 设置输入文本的类型
     *
     * @param type
     */
    public void setInputType(int type) {
        edInput.setInputType(type);
    }

    public void setInputHintColor(int color) {
        edInput.setHintTextColor(color);
    }

    public void setInputHintText(int text) {
        edInput.setHint(text);
    }

    public CharSequence getText() {
        return edInput.getText();
    }

    public void setText(CharSequence text) {
        edInput.setText(text);
    }

    public void setInputMaxLength(int maxLength) {
        edInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
    }

    public void setSelection(int index) {
        edInput.setSelection(index);
    }

    @Override
    public void clearFocus() {
        super.clearFocus();
        edInput.clearFocus();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(animator != null) {
            animator.cancel();
            animator = null;
        }
    }

    private ValueAnimator animator;

    public void showByAnim() {
        viewLine.setPivotX(0);
        animator = ValueAnimator.ofFloat(0,1f).setDuration(400);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float p = animation.getAnimatedFraction();

                viewLine.setScaleX(p);
                imgIcon.setAlpha(p);
                edInput.setAlpha(p);
                edInput.setTranslationX(edInput.getWidth() * (1 - p));

            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                viewLine.setScaleX(1f);
                imgIcon.setAlpha(1f);
                edInput.setAlpha(1f);
                edInput.setTranslationX(0);
            }
        });
        animator.start();
    }
}
